clear all
capture log close
capture set virtual off
set memory 1000m
set memory 1000m
capture set matsize 100000000000
capture set more off
capture virtual off
memory

set type double
set more off
set maxvar 32767
set matsize 11000
pause on
version 12.1


cd "/Users/Mandywu/Dropbox (MIT)/PNAS revision/code"

use campaign_all_10wk_nt, replace

gen pre_campaign = campaigntime <= -5
gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0
gen campaign_post = campaigntime2 >= 1
save campaign_sum, replace

***************** code for table 3 city-level standard error
use campaign_all_10wk_nt, replace

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

**** calculate for orginial
forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_post, ///
absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table3_eb.tex, replace keep(campaign_prepare campaign_onsite campaign_post) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes, Round FE, Yes) dec(3)


use campaign_all_10wk_match_nt, replace
gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_post [pweight=_webal], ///
absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table3_eb.tex, append keep(campaign_prepare campaign_onsite campaign_post) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes, Round FE, Yes) dec(3)

***** breakdown of period
use campaign_all_10wk_nt, replace
gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2, ///
absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table3_eb.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes, Round FE, Yes) dec(3)


use campaign_all_10wk_match_nt, replace
gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2  [pweight=_webal], ///
absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table3_eb.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes, Round FE, Yes) dec(3)


***************** code for table 4_eb ***** city-standard error
*** All
use campaign_all_10wk_match_nt, replace

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table4_eb.tex, replace keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes) dec(3)


**** central soe
use campaign_all_10wk_match_nt, replace

keep if  central_soe == 1

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table4_eb.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes) dec(3)

**** city and lower
use campaign_all_10wk_match_nt, replace

keep if state_lower == 1

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table4_eb.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes) dec(3)




***************** code for table 5_eb ***** city-standard error
*** All
use campaign_all_10wk_match_nt, replace

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

gen campaign_post_so2 = campaign_post * so2_scrubber
gen campaign_onsite_so2 = campaign_onsite * so2_scrubber
gen campaign_rebound1_so2 = campaign_rebound1 * so2_scrubber
gen campaign_rebound2_so2 = campaign_rebound2 * so2_scrubber

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 ///
campaign_onsite_so2 campaign_rebound1_so2 campaign_rebound2_so2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table5.tex, replace keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 campaign_onsite_so2 campaign_rebound1_so2 campaign_rebound2_so2) ///
nocons addtext(Plant FE, Yes, Year-Week FE, Yes) dec(3)


*** central soe
use campaign_all_10wk_match_nt, replace

keep if  central_soe == 1
gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

gen campaign_post_so2 = campaign_post * so2_scrubber
gen campaign_onsite_so2 = campaign_onsite * so2_scrubber
gen campaign_rebound1_so2 = campaign_rebound1 * so2_scrubber
gen campaign_rebound2_so2 = campaign_rebound2 * so2_scrubber

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 ///
campaign_onsite_so2 campaign_rebound1_so2 campaign_rebound2_so2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table5.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 campaign_onsite_so2 campaign_rebound1_so2 campaign_rebound2_so2) ///
nocons addtext(Plant FE, Yes, Year-Week FE, Yes) dec(3)


**** city and lower
use campaign_all_10wk_match_nt, replace

keep if state_lower == 1
gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1


gen campaign_post_so2 = campaign_post * so2_scrubber
gen campaign_onsite_so2 = campaign_onsite * so2_scrubber
gen campaign_rebound1_so2 = campaign_rebound1 * so2_scrubber
gen campaign_rebound2_so2 = campaign_rebound2 * so2_scrubber

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_SO2 campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 ///
campaign_onsite_so2 campaign_rebound1_so2 campaign_rebound2_so2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table5.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 campaign_onsite_so2 campaign_rebound1_so2 campaign_rebound2_so2) ///
nocons addtext(Plant FE, Yes, Year-Week FE, Yes) dec(3)


***************** code for NOX result ***** city-standard error
*** central soe
use campaign_all_10wk_match_nt, replace
keep if nox_scrubber == 0

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_NOX campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table5.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes) dec(3)



*** central soe
use campaign_all_10wk_match_nt, replace
keep if nox_scrubber == 0
keep if  central_soe == 1

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_NOX campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table5.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes) dec(3)

**** city and lower
use campaign_all_10wk_match_nt, replace
keep if nox_scrubber == 0
keep if state_lower == 1

gen campaign_prepare = campaigntime >= -4 & campaigntime <= -1 & campaign_treated == 1
gen campaign_onsite = campaigntime >= 0 & campaigntime2 <= 0 & campaign_treated == 1
gen campaign_post = campaigntime2 >= 1 & campaign_treated == 1
gen campaign_rebound1 = campaigntime2 >= 1 & campaigntime2 <= 5 & campaign_treated == 1
gen campaign_rebound2 = campaigntime2 >= 6 & campaigntime2 <= 10 & campaign_treated == 1
sort plant_name yweek
by plant_name: egen weekmin= min(yweek)
gen t= yweek - weekmin + 1

forval o=0/2 {
eststo y`o':  reghdfe log_concentration_NOX campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2 [pweight=_webal], absorb(plant_name1 yweek) vce(cluster city_name)
}
eststo linear_plant_time_trend
outreg2 using table5.tex, append keep(campaign_prepare campaign_onsite campaign_rebound1 campaign_rebound2) ///
nocons addtext(Plant FE, Yes, Year FE, Yes, Week FE, Yes) dec(3)




**********
********* csdid desgin

use panel_plant_weekly_diff_control, replace

encode plant_name, generate(plant_name1)
encode city_name, generate(city_name1)
gen campaign_date = 0

replace campaign_date = yweek if (first_period==1 & (province_name == "河南省" | province_name == "黑龙江省")) | ///
(second_period==1 & (province_name == "广东省" | province_name=="湖北省")) | ///
(third_period==1 & (province_name == "山西省" | province_name =="湖南省")) | ///
(fourth_period==1  & (province_name == "山东省" | province_name=="浙江省"))

sort plant_name yweek
by plant_name: gen weeknum1=_n
by plant_name: gen target1=weeknum1 if yweek==campaign_date
egen td1=min(target1), by(plant_name)
gen campaigntime=weeknum1-td1
egen td2=max(target1), by(plant_name)
gen campaigntime2=weeknum1-td2
*drop weeknum1 target1 td1 td2
gen baseline = td1 - 5
rename td1 first_treat

*keep if campaigntime >= -30 & campaigntime2 <= 30
keep if year >= 2016 & year <= 2017

save campaign_est_baseline.dta, replace

*** here the treatment week is the first week 
csdid  log_concentration_SO2 per_capita_log, ivar(plant_name1) time(yweek) gvar(baseline) method(dripw) cluster(city_name1)

*** get ATT coefficients
estat cevent, window(1 4) estore(announcement)
*** ATT for events between -4 to -1 week
estat cevent, window(5 9) estore(onsite)
*** ATT for events between 0 to 4 week
estat cevent, window(10 19) estore(post)
estat cevent, window(10 14) estore(rebound1)
estat cevent, window(15 19) estore(rebound2)

esttab announcement onsite post using table3_csdid.tex, star(* 0.10 ** 0.05 *** 0.01) se replace
esttab announcement onsite rebound1 rebound2 using table3_csdid.tex, star(* 0.10 ** 0.05 *** 0.01) se append


**** table 4_csdid
***All
use campaign_est_baseline, replace

csdid  log_concentration_SO2, ivar(plant_name1) time(yweek) gvar(baseline) method(dripw) cluster(city_name1)

estat cevent, window(1 4) estore(announcement)
*** ATT for events between -4 to -1 week
estat cevent, window(5 9) estore(onsite)
*** ATT for events between 0 to 4 week
*estat cevent, window(10 19) estore(post)
estat cevent, window(10 14) estore(rebound1)
estat cevent, window(15 19) estore(rebound2)

esttab announcement onsite rebound1 rebound2 using table4_csdid.tex, se replace

*** central soe
use campaign_est_baseline, replace
gen central_soe = 0
replace central_soe = 1 if oversightlevel == "country-level" & soe == 1

keep if  central_soe == 1
csdid  log_concentration_SO2, ivar(plant_name1) time(yweek) gvar(baseline) method(dripw) cluster(city_name1)

estat cevent, window(1 4) estore(announcement)
*** ATT for events between -4 to -1 week
estat cevent, window(5 9) estore(onsite)
*** ATT for events between 0 to 4 week
*estat cevent, window(10 19) estore(post)
estat cevent, window(10 14) estore(rebound1)
estat cevent, window(15 19) estore(rebound2)

esttab announcement onsite rebound1 rebound2 using table4_csdid.tex, star(* 0.10 ** 0.05 *** 0.01) se append

**** city and below
use campaign_est_baseline, replace
gen lower_soe = 0
replace lower_soe = 1 if (oversightlevel == "county-level" | oversightlevel == "city-level") & soe == 1

keep if  lower_soe == 1
csdid  log_concentration_SO2, ivar(plant_name1) time(yweek) gvar(baseline) method(dripw) cluster(city_name1)

estat cevent, window(1 4) estore(announcement)
*** ATT for events between -4 to -1 week
estat cevent, window(5 9) estore(onsite)
*** ATT for events between 0 to 4 week
*estat cevent, window(10 19) estore(post)
estat cevent, window(10 14) estore(rebound1)
estat cevent, window(15 19) estore(rebound2)

esttab announcement onsite rebound1 rebound2 using table4_csdid.tex, star(* 0.10 ** 0.05 *** 0.01) se append
